package com.webbei.HAO.action;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.opensymphony.xwork2.ActionSupport;
import com.webbei.HAO.service.ChSer;

public class Findpwd1 extends ActionSupport{
     private String mail;
     private ChSer ser;
     private String str;
     private String fan;
	public String execute(){
		
	   if(mail.equals("")||mail==null){
		 str="邮箱不能为空";
		 fan="error";
	   }else{
		   if(ser.customercheck(mail)!=0)
			  fan="success";
		   if(fan!="success") {fan="error";str="该用户不存在";}
		   else{
			   try {  
		           Properties p = new Properties(); //Properties p = System.getProperties();  
		            p.put("mail.smtp.auth", "true");  
		            p.put("mail.transport.protocol", "smtp");  
		            p.put("mail.smtp.host", "smtp.163.com");  
		            p.put("mail.smtp.port", "25");  
		            //建立会话  
		            Session session = Session.getInstance(p);  
		            Message msg = new MimeMessage(session); //建立信息  
		   
		            msg.setFrom(new InternetAddress("chenhao01122@163.com")); //发件人  
		            msg.setRecipient(Message.RecipientType.TO,  
		                             new InternetAddress(mail)); //收件人  
		   
		            msg.setSentDate(new Date()); // 发送日期  
		            msg.setSubject("找回密码"); // 主题  
		            msg.setText("密码是："+ser.getPwdByMail(mail)); //内容  
		            // 邮件服务器进行验证  
		            Transport tran = session.getTransport("smtp");  
		            tran.connect("smtp.163.com", "chenhao01122", "02040204");  
		            // bluebit_cn是用户名，xiaohao是密码  
		            tran.sendMessage(msg, msg.getAllRecipients()); // 发送  
		            System.out.println("邮件发送成功");  
		   
		        } catch (Exception e) {  
		            e.printStackTrace();  
		        }  
		   }
	   }
	   
	   return fan;
   }
	public String getMail() {
		return mail;
	}
	public void setMail(String mail) {
		this.mail = mail;
	}
	public ChSer getSer() {
		return ser;
	}
	public void setSer(ChSer ser) {
		this.ser = ser;
	}
	public String getStr() {
		return str;
	}
	public void setStr(String str) {
		this.str = str;
	}
	
}
